<?php
		
		$id=$_GET['id']; //string nha
		
		$db=new db;
		$db->connect();
		$id=mysql_real_escape_string($id);
		//-------------------------- GET TRUYEN ----------------
		$db->query("select * from anime_tag1 where alias='".$id."'");
		if($db->num_rows()>0){
				$r1=$db->fetchOne();
				$data['tag']=$r1;
				$sql ="select * from anime_tag1_fk where tag_id=".$r1['id']." order by anime_id desc";
				$db->query($sql);
				
				
				if($db->num_rows()==1){	
						$r1=$db->fetchOne();
						$data['num']=1;
						$anime=cache_get_anime($r1['anime_id']);
						$data['anime']=$anime;
						//Get 3 ep moi nhat ne, connect lại db vi bị close ở trên
						$db=new db;
						$db->connect();
						$db->query("select id,fansub_id from anime_ep where anime_id=".$r1['anime_id']." order by thutu desc limit 0,4");
						
						if($db->num_rows()>0){	
						
							$r2=$db->fetchAll();
							
							foreach($r2 as $row){
								$r3['fansub']=cache_get_fansub($row['fansub_id']);
							$r3['ep']=cache_get_ep($row['id']);
								$data['ep'][]=$r3;
							}
						}
				}else{ //Else of num_rows==1
					$cache=new cache;
					$data['num']=2;
					$sql ="select anime_id from anime_tag1_fk where tag_id=".$r1['id']." order by anime_id desc";
					echo $sql;
					$sql2="select id from anime_tag2_fk where tag_id=".$r1['id'];
					$pager = new Pagination($sql,16,4,'',0,'anime_2');
					$pager->setDebug(false);
					$data['info'] = $pager->paginate($cache,$sql2);
					
				
				} //End of else num_rows==1
		
		//Ta co ep_id,anime_id
		  //LAY RA ANIME INFO,EP_INFO
		/*  echo "<pre>";
		  print_r($data);
		  echo "</pre>";*/
		}else{ //End of if
		//Khong co trong db
		$r1['name']=$_GET['id'];
		$r1['alias']=khongdau($_GET['id']);
		$data['tag']=$r1;
		
		}

	
	
			
			$data1[title]=web_title($id."| Kết quả tìm kiếm | Anime24h.vn - Xem Anime Online");
			$data1['mota']="Kết quả tìm kiêm từ khóa ".$id;
			$data1['keyword']=$id;
			loadview('template/header',$data1);
			loadview("truyen/tag",$data);
			
			
